import path from 'path'
import fs from 'fs'

import ts from '@rollup/plugin-typescript'
import cjs from '@rollup/plugin-commonjs'
import nodeResolve from '@rollup/plugin-node-resolve'

const pkgPath = path.resolve(__dirname, 'packages')
const distPath = path.resolve(__dirname, 'dist/node_modules')

export function resolvePkgPath(pkgName, isDist) {
  if (isDist) {
    return path.resolve(distPath, pkgName)
  }
  return path.resolve(pkgPath, pkgName)
}

export function getPackageJSON(pkgName) {
  // ...包路径
  const path = `${resolvePkgPath(pkgName)}/package.json`
  const str = fs.readFileSync(path, { encoding: 'utf-8' })
  return JSON.parse(str)
}

export function getBaseRollupPlugins({ typescript = {} } = {}) {
  return [nodeResolve(), cjs(), ts(typescript)]
}
